home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / ppm / ppmtopi1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  3.2 KB  |  122 lines

  1. /* ppmtopi1.c - read a portable pixmap and write a Degas PI1 file
  2. **
  3. ** Copyright (C) 1991 by Steve Belczyk and Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14. #include "ppmcmap.h"
  15.  
  16. #define COLS 320
  17. #define ROWS 200
  18. #define MAXVAL 7
  19. #define MAXCOLORS 16
  20.  
  21. void
  22. main( argc, argv )
  23.     int argc;
  24.     char* argv[];
  25.     {
  26.     FILE* ifp;
  27.     pixel** pixels;
  28.     register pixel *pP;
  29.     colorhist_vector chv;
  30.     colorhash_table cht;
  31.     int rows, cols, row, colors, i;
  32.     register int col;
  33.     pixval maxval;
  34.     short screen[ROWS*COLS/4];    /* simulate the ST's video RAM */
  35.  
  36.     ppm_init( &argc, argv );
  37.  
  38.     if ( argc > 2 )
  39.     pm_usage( "[ppmfile]" );
  40.  
  41.     if ( argc == 2 )
  42.     ifp = pm_openr( argv[1] );
  43.     else
  44.     ifp = stdin;
  45.  
  46.     pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
  47.     pm_close( ifp );
  48.     if ( (cols > COLS) || (rows > ROWS) )
  49.     pm_error( "image is larger than %dx%d - sorry", COLS, ROWS );
  50.  
  51.     pm_message( "computing colormap..." );
  52.     chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors );
  53.     if ( chv == (colorhist_vector) 0 )
  54.     {
  55.     pm_message(
  56.         "too many colors - try doing a 'ppmquant %d'", MAXCOLORS );
  57.     exit( 1 );
  58.     }
  59.     pm_message( "%d colors found", colors );
  60.  
  61.     /* Write PI1 header - resolution and palette. */
  62.     (void) pm_writebigshort( stdout, (short) 0 );    /* low resolution */
  63.     for ( i = 0; i < 16; ++i )
  64.     {
  65.     short w;
  66.  
  67.     if ( i < colors )
  68.         {
  69.         pixel p;
  70.  
  71.         p = chv[i].color;
  72.         if ( maxval != MAXVAL )
  73.         PPM_DEPTH( p, p, maxval, MAXVAL );
  74.         w  = ( (int) PPM_GETR( p ) ) << 8;
  75.         w |= ( (int) PPM_GETG( p ) ) << 4;
  76.         w |= ( (int) PPM_GETB( p ) );
  77.         }
  78.     else
  79.         w = 0;
  80.     (void) pm_writebigshort( stdout, w );
  81.     }
  82.     if ( maxval > MAXVAL )
  83.     pm_message(
  84.         "maxval is not %d - automatically rescaling colors", MAXVAL );
  85.  
  86.     /* Convert color vector to color hash table, for fast lookup. */
  87.     cht = ppm_colorhisttocolorhash( chv, colors );
  88.     ppm_freecolorhist( chv );
  89.  
  90.     /* Clear the screen buffer. */
  91.     for ( i = 0; i < ROWS*COLS/4; ++i )
  92.     screen[i] = 0;
  93.  
  94.     /* Convert. */
  95.     for ( row = 0; row < rows; ++row )
  96.     {
  97.     for ( col = 0, pP = pixels[row]; col < cols; ++col, ++pP )
  98.         {
  99.         register int color, ind, b, plane;
  100.  
  101.         color = ppm_lookupcolor( cht, pP );
  102.         if ( color == -1 )
  103.         pm_error(
  104.             "color not found?!?  row=%d col=%d  r=%d g=%d b=%d",
  105.             row, col, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP) );
  106.         ind = 80 * row + ( ( col >> 4 ) << 2 );
  107.         b = 0x8000 >> (col & 0xf);
  108.         for ( plane = 0; plane < 4; ++plane )
  109.         if ( color & (1 << plane) )
  110.             screen[ind+plane] |= b;
  111.         }
  112.     }
  113.  
  114.     /* And write out the screen buffer. */
  115.     for ( i = 0; i < ROWS*COLS/4; ++i )
  116.     (void) pm_writebigshort( stdout, screen[i] );
  117.  
  118.     pm_close (stdout);
  119.  
  120.     exit( 0 );
  121.     }
  122.